/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui.report;

import easybooking.ReportClientController;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Tim Schröder
 * http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting
 */
public class RCCustomerForm extends JPanel {

    private ReportClientController rcc;
    private JLabel lblSearch;
    private JTextField txfSearch;
    private JTable tblCustomers;
    private JPanel buttonPanel;
    private JButton btnCreateCustomer;
    private Vector tableData;
    private TableRowSorter<RCCustomerTableModel> sorter;
    private RCCustomerTableModel model;
    private JLabel lblLogo;

    /**
     * Constructor of the ReportClientController class, which sets the
     * controller
     * @param rcif the ReportClientController
     */
    public RCCustomerForm(ReportClientController rcif) {
        rcc = rcif;
    }

    /**
     * get the inserted text from the search text field
     * @return the searched text
     */
    public String getSearchText() {
        return txfSearch.getText();
    }

    /**
     * gets and returns the selected customer from the table / table model
     * @return the selected customer
     */
    public Object getSelectedCustomer() {
        if (tblCustomers.getSelectedRow() == -1) {
            return null;
        }
        int modelRow = tblCustomers.convertRowIndexToModel(tblCustomers.getSelectedRow());
        return tblCustomers.getModel().getValueAt(modelRow, 6);
    }

    /**
     * sets the data for the customer table model
     * @param v is the vector containing all costomers and is used to create the table
     */
    public void setTableData(Vector v) {
        tableData = v;
    }

    /**
     * calls the generateComponents() method to create and show the CostomerForm
     */
    public void createAndShowForm() {
        generateComponents();
    }

    /**
     * Generates all components to create an display the GUI
     */
    private void generateComponents() {
        // sets system default look and feel
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }

        this.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Manage Customers "),
                BorderFactory.createEmptyBorder(20, 20, 20, 20)));

        this.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        lblSearch = new JLabel("Search:");
        // Places the GUI elements in the layout using addComponent                                  x, y, w, h, wx, wy
        addComponent(this, gbl, lblSearch, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);
        txfSearch = new JTextField(35);

        // registrates the document listener for filtering
        txfSearch.getDocument().addDocumentListener(
            new DocumentListener() {
                public void changedUpdate(DocumentEvent e) {
                    customerFilter();
                }

                public void insertUpdate(DocumentEvent e) {
                    customerFilter();
                }

                public void removeUpdate(DocumentEvent e) {
                    customerFilter();
                }
            });
        // Places the GUI elements in the layout using addComponent                                  x, y, w, h, wx, wy
        addComponent(this, gbl, txfSearch, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 0, 1, 1, 0, 0);

        lblLogo = new JLabel();
        Icon Logo = new ImageIcon(this.getClass().getResource("/easybooking/clients/gui/images/easyBookingLogoSmall2.png"));
        lblLogo.setIcon(Logo);
        addComponent(this, gbl, lblLogo, GridBagConstraints.BOTH, GridBagConstraints.EAST, 2, 0, 4, 1, 1, 0);

        // creates table with customer model and data
        tblCustomers = new JTable();
        // gets an instance of the customerTableModel with table data
        model = new RCCustomerTableModel(tableData);

        // creates the sorter for the table depending on the table model
        sorter = new TableRowSorter<RCCustomerTableModel>(model);

        // sets the model of tblCustomers
        tblCustomers.setModel(model);

        // adds the sorter to tblCustomers
        tblCustomers.setRowSorter(sorter);

        // allows only a single selection on the table
        tblCustomers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        // adds the table within a scroll pane to the layout
        // Places the GUI elements in the layout using addComponent                                                x, y, w, h, wx, wy
        addComponent(this, gbl, new JScrollPane(tblCustomers), GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0, 1, 3, 1, 1, 1);

        // adds the buttton panel to the layout
        // Places the GUI elements in the layout using addComponent                                                 x, y, w, h, wx, wy
        addComponent(this, gbl, this.createButtonPanel(), GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 2, 3, 1, 0, 0);
    }

    /**
     * Creates a tablerowfilter for the CustomerTableModle based on ragular expressions and adds it to the sorter
     */
    private void customerFilter() {
        RowFilter<RCCustomerTableModel, Object> rf = null;
        //If current expression doesn't parse, don't update.
        try {
            //                                              columns included in filter
            rf = RowFilter.regexFilter(txfSearch.getText(), 0, 1, 2, 3, 4, 5);

        } catch (java.util.regex.PatternSyntaxException e) {
            return;
        }
        sorter.setRowFilter(rf);
    }

    /**
     * creates a a button to create a customer report
     * @return a JPanal with two buttons
     */
    private JPanel createButtonPanel() {

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        btnCreateCustomer = new JButton("Create Report");
        //Icon nCuIcon = new ImageIcon(RCCustomerForm.class.getResource("./images/create.png"));
        //btnCreateCustomer.setIcon(nCuIcon);
        btnCreateCustomer.setActionCommand("createCustomerReport");
        btnCreateCustomer.addActionListener(rcc);

        // Adds the buttons to the buttonPanel
        addComponent(buttonPanel, gbl, btnCreateCustomer, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 0, 3, 1, 1, 0);

        return buttonPanel;
    }
    // Adds the elements to the layout

    /**
     * defines the layout of the GUI
     * @param cont container to which the component is added
     * @param gbl GridBagLayout object
     * @param c compontent which is added to the container
     * @param fill how to use free space in the cell around the component
     * @param anchor where to fix an align the component
     * @param x x position in the grid layout
     * @param y y position in the grid layout
     * @param width number of columns the element should spread
     * @param hight number of rows the element should spread
     * @param weightx handels the horizontal free space
     * @param weighty handels the vertical free space
     */
    private static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);


    }
}
